<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Classes extends Backend_Controller {

	protected $_title = 'Kelas';

	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="admin")
			redirect('login/index/1');
		$this->load->model('classes_model', 'classes', true);
	}
		
	public function index($offset=0, $sort_by='name') {
		
		$data = array(
			'mainview' => 'index',
		) + $this->_get_header_data();
		
		$this->load->view('admin/template',$data);
		
	}
	
	function _crud_output($output = null) {
        $this->load->view('admin/template_crud.php',$output);    
    }

	function classes_display() 	{	
		$crud = new grocery_CRUD();
        $crud->set_theme('flexigrid');
        $crud->set_table('classes');
        $crud->set_subject('Kelas');
        
        $crud->columns('name','room_code');
		$crud->display_as('name','Nama Ruang');
		$crud->display_as('room_code','Kode Ruang');
		$crud->required_fields('name');
		$crud->fields('name','room_code','school_year_id');
		$crud->change_field_type('school_year_id','invisible');
		
		$crud->callback_before_insert(array($this, 'insert_school_year'));
		$crud->callback_before_update(array($this, 'insert_school_year'));
		
		$crud->set_rules('name', 'name','callback_class_check');
        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	public function class_check($str)
	{
		$rs = $this->classes->get_by_name($str);
		if( count($rs)>0 ) 
		{
			$this->form_validation->set_message('class_check', 'Nama kelas sudah ada.');
			return FALSE;
		}
		else
			return TRUE;
	}
	
	function insert_school_year($post_array,$primary_key)
	{
		$post_array['school_year_id'] = $this->classes->get_current_school_year_id();
		return $post_array;
	}
}
